SlideShare a Scribd company logo
Class No.25  Data Structures http://ecomputernotes.com
BuildHeap The general algorithm is to place the N keys in an array and consider it to be an unordered binary tree. The following algorithm will build a heap out of N keys. for( i = N/2; i > 0; i-- ) percolateDown(i); http://ecomputernotes.com
BuildHeap i = 15/2 = 7 65 19 21 14 24 26 13 15 68 16 65 21 19 26 14 16 68 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 5 13 70 14 12 15 5 70 12 i   i Why I=n/2? http://ecomputernotes.com
BuildHeap i = 15/2 = 7 65 19 21 14 24 26 13 15 12 16 65 21 19 26 14 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 5 13 70 14 68 15 5 70 68 i   i http://ecomputernotes.com
BuildHeap i = 6 65 19 21 14 24 26 13 15 12 16 65 21 19 26 14 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 5 13 70 14 68 15 5 70 68 i   i http://ecomputernotes.com
BuildHeap i = 5 65 5 21 14 24 26 13 15 12 16 65 21 5 26 14 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 19 13 70 14 68 15 19 70 68 i   i http://ecomputernotes.com
BuildHeap i = 4 65 5 14 21 24 26 13 15 12 16 65 14 5 26 21 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 19 13 70 14 68 15 19 70 68 i   i http://ecomputernotes.com
BuildHeap i = 3 65 5 14 21 24 13 26 15 12 16 65 14 5 13 21 16 12 26 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 19 13 70 14 68 15 19 70 68 i   i http://ecomputernotes.com
BuildHeap i = 2 65 16 14 21 24 13 26 15 12 32 65 14 16 13 21 32 12 26 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 5 5 12 15 4 19 13 70 14 68 15 19 70 68 i   i http://ecomputernotes.com
BuildHeap i = 1 65 16 14 21 31 24 26 15 12 32 65 14 16 24 21 32 12 26 31 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 13 13 11 5 5 12 15 4 19 13 70 14 68 15 19 70 68 i   i http://ecomputernotes.com
BuildHeap Min heap 5 16 14 21 31 24 26 15 65 32 5 14 16 24 21 32 65 26 31 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 13 13 11 12 12 12 15 4 19 13 70 14 68 15 19 70 68 http://ecomputernotes.com
Other Heap Operations decreaseKey(p, delta) :  lowers the value of the key at position ‘p’ by the amount ‘delta’. Since this might violate the heap order, the heap must be reorganized with percolate up (in min heap) or down (in max heap). increaseKey(p, delta) :  opposite of decreaseKey. remove(p) :  removes the node at position p from the heap. This is done by first decreaseKey(p, ) and then performing deleteMin(). http://ecomputernotes.com
Heap code in C++ template <class eType> class Heap { public: Heap( int capacity = 100 ); void insert( const eType & x ); void deleteMin( eType & minItem ); const eType & getMin( ); bool isEmpty( ); bool isFull( ); int Heap<eType>::getSize( ); http://ecomputernotes.com
Heap code in C++ private: int currentSize; // Number of elements in heap eType* array; // The heap array int capacity; void percolateDown( int hole ); }; http://ecomputernotes.com
Heap code in C++ #include &quot;Heap.h“ template <class eType> Heap<eType>::Heap( int capacity ) { array = new etype[capacity + 1]; currentSize=0; } http://ecomputernotes.com
Heap code in C++ // Insert item x into the heap, maintaining heap // order. Duplicates are allowed. template <class eType> bool Heap<eType>::insert( const eType & x ) { if( isFull( ) ) { cout << &quot;insert - Heap is full.&quot; << endl; return 0; } // Percolate up int hole = ++currentSize; for(; hole > 1 && x < array[hole/2 ]; hole /= 2) array[ hole ] = array[ hole / 2 ]; array[hole] = x; } http://ecomputernotes.com
Heap code in C++ template <class eType> void Heap<eType>::deleteMin( eType & minItem ) { if( isEmpty( ) ) { cout <<  &quot; heap is empty. “ << endl ; return; } minItem = array[ 1 ]; array[ 1 ] = array[ currentSize-- ]; percolateDown( 1 ); } http://ecomputernotes.com

More Related Content

PPT
Computer notes - Build Heap
ODP
C workshop day 6
DOCX
แผนการจัดการเรียนรู้ที่ ๓
TXT
Influencias admon
DOCX
Wap to implement bitwise operators
PDF
Practicas 1.4
PDF
Raphaël and You
PPTX
Device Modeling of 3INPUT COMPARATOR using PSpice
Computer notes - Build Heap
C workshop day 6
แผนการจัดการเรียนรู้ที่ ๓
Influencias admon
Wap to implement bitwise operators
Practicas 1.4
Raphaël and You
Device Modeling of 3INPUT COMPARATOR using PSpice

What's hot (14)

PPT
Tilting Google Maps and MissileLauncher
PDF
Seaborn graphing present
PDF
Exercicis selectivitat
PDF
AI&ML Conference 2019 - Deep Learning from zero to hero
PDF
Clock For My
PPT
Open Cv Tutorial Ii
PDF
TC7S08F PSpice Model (Free SPICE Model)
DOC
PDF
Advanced Topics in Haskell
PDF
Predictive Analytics Powered By Process Mining: It’s The Process, Stupid!
KEY
真っ黒Scheme
PPTX
Self Evaluation Test
Tilting Google Maps and MissileLauncher
Seaborn graphing present
Exercicis selectivitat
AI&ML Conference 2019 - Deep Learning from zero to hero
Clock For My
Open Cv Tutorial Ii
TC7S08F PSpice Model (Free SPICE Model)
Advanced Topics in Haskell
Predictive Analytics Powered By Process Mining: It’s The Process, Stupid!
真っ黒Scheme
Self Evaluation Test
Ad

Viewers also liked (20)

PPT
computer notes - Data Structures - 20
PPT
computer notes - Data Structures - 17
PPT
computer notes - Data Structures - 7
PPT
computer notes - Data Structures - 5
PPT
computer notes - Data Structures - 29
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 26
PPT
computer notes - Data Structures - 8
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 24
PPT
computer notes - Data Structures - 31
PPT
computer notes - Data Structures - 34
PPT
computer notes - Data Structures - 32
PPT
computer notes - Data Structures - 10
PPT
computer notes - Data Structures - 13
PPT
computer notes - Data Structures - 15
PPT
computer notes - Data Structures - 18
PPT
computer notes - Data Structures - 27
PPT
computer notes - Data Structures - 9
PPT
computer notes - Data Structures - 1
computer notes - Data Structures - 20
computer notes - Data Structures - 17
computer notes - Data Structures - 7
computer notes - Data Structures - 5
computer notes - Data Structures - 29
computer notes - Data Structures - 22
computer notes - Data Structures - 26
computer notes - Data Structures - 8
computer notes - Data Structures - 11
computer notes - Data Structures - 24
computer notes - Data Structures - 31
computer notes - Data Structures - 34
computer notes - Data Structures - 32
computer notes - Data Structures - 10
computer notes - Data Structures - 13
computer notes - Data Structures - 15
computer notes - Data Structures - 18
computer notes - Data Structures - 27
computer notes - Data Structures - 9
computer notes - Data Structures - 1
Ad

Similar to computer notes - Data Structures - 25 (20)

PPT
Computer notes data structures - 9
PDF
LEC 8-DS ALGO(heaps).pdf
PPT
Computer notes - Mergesort
PPTX
(Slightly) Smarter Smart Pointers
PPT
Computer notes - Sorting
PPT
computer notes - Data Structures - 38
PPT
computer notes - Data Structures - 39
PPTX
Build Heap Step by Step
PDF
How to master C++
TXT
Saii log
PDF
The Ring programming language version 1.6 book - Part 79 of 189
PDF
Fast and accurate metrics. Is it actually possible?
PDF
ps with notes required to tackelt project
DOCX
Fatima Aliasgher Portfolio
PDF
CGo for fun and profit
PPT
computer notes - Data Structures - 16
PPTX
리눅스 드라이버 실습 #3
PDF
Everything you didn't know you needed
PDF
Creating Responsive Experiences
KEY
10 Catalyst Tips
Computer notes data structures - 9
LEC 8-DS ALGO(heaps).pdf
Computer notes - Mergesort
(Slightly) Smarter Smart Pointers
Computer notes - Sorting
computer notes - Data Structures - 38
computer notes - Data Structures - 39
Build Heap Step by Step
How to master C++
Saii log
The Ring programming language version 1.6 book - Part 79 of 189
Fast and accurate metrics. Is it actually possible?
ps with notes required to tackelt project
Fatima Aliasgher Portfolio
CGo for fun and profit
computer notes - Data Structures - 16
리눅스 드라이버 실습 #3
Everything you didn't know you needed
Creating Responsive Experiences
10 Catalyst Tips

More from ecomputernotes (18)

PPT
computer notes - Data Structures - 30
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 4
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 35
PPT
computer notes - Data Structures - 36
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
PPT
computer notes - Data Structures - 14
DOC
Computer notes - Controlling User Access
DOC
Computer notes - Using SET Operator
computer notes - Data Structures - 30
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 28
computer notes - Data Structures - 19
computer notes - Data Structures - 4
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 35
computer notes - Data Structures - 36
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 14
Computer notes - Controlling User Access
Computer notes - Using SET Operator

Recently uploaded (20)

PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Encapsulation theory and applications.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
KodekX | Application Modernization Development
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Approach and Philosophy of On baking technology
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Big Data Technologies - Introduction.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Programs and apps: productivity, graphics, security and other tools
Diabetes mellitus diagnosis method based random forest with bat algorithm
Encapsulation theory and applications.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The AUB Centre for AI in Media Proposal.docx
MIND Revenue Release Quarter 2 2025 Press Release
Chapter 3 Spatial Domain Image Processing.pdf
KodekX | Application Modernization Development
Building Integrated photovoltaic BIPV_UPV.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Approach and Philosophy of On baking technology
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Network Security Unit 5.pdf for BCA BBA.
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Spectral efficient network and resource selection model in 5G networks
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Big Data Technologies - Introduction.pptx
cuic standard and advanced reporting.pdf
Encapsulation_ Review paper, used for researhc scholars
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Programs and apps: productivity, graphics, security and other tools

computer notes - Data Structures - 25

  • 1. Class No.25 Data Structures http://ecomputernotes.com
  • 2. BuildHeap The general algorithm is to place the N keys in an array and consider it to be an unordered binary tree. The following algorithm will build a heap out of N keys. for( i = N/2; i > 0; i-- ) percolateDown(i); http://ecomputernotes.com
  • 3. BuildHeap i = 15/2 = 7 65 19 21 14 24 26 13 15 68 16 65 21 19 26 14 16 68 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 5 13 70 14 12 15 5 70 12 i  i Why I=n/2? http://ecomputernotes.com
  • 4. BuildHeap i = 15/2 = 7 65 19 21 14 24 26 13 15 12 16 65 21 19 26 14 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 5 13 70 14 68 15 5 70 68 i  i http://ecomputernotes.com
  • 5. BuildHeap i = 6 65 19 21 14 24 26 13 15 12 16 65 21 19 26 14 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 5 13 70 14 68 15 5 70 68 i  i http://ecomputernotes.com
  • 6. BuildHeap i = 5 65 5 21 14 24 26 13 15 12 16 65 21 5 26 14 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 19 13 70 14 68 15 19 70 68 i  i http://ecomputernotes.com
  • 7. BuildHeap i = 4 65 5 14 21 24 26 13 15 12 16 65 14 5 26 21 16 12 13 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 19 13 70 14 68 15 19 70 68 i  i http://ecomputernotes.com
  • 8. BuildHeap i = 3 65 5 14 21 24 13 26 15 12 16 65 14 5 13 21 16 12 26 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 32 32 12 15 4 19 13 70 14 68 15 19 70 68 i  i http://ecomputernotes.com
  • 9. BuildHeap i = 2 65 16 14 21 24 13 26 15 12 32 65 14 16 13 21 32 12 26 24 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 31 31 11 5 5 12 15 4 19 13 70 14 68 15 19 70 68 i  i http://ecomputernotes.com
  • 10. BuildHeap i = 1 65 16 14 21 31 24 26 15 12 32 65 14 16 24 21 32 12 26 31 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 13 13 11 5 5 12 15 4 19 13 70 14 68 15 19 70 68 i  i http://ecomputernotes.com
  • 11. BuildHeap Min heap 5 16 14 21 31 24 26 15 65 32 5 14 16 24 21 32 65 26 31 15 1 2 3 5 6 7 8 9 10 11 12 13 14 0 1 2 3 7 6 5 4 8 9 10 13 13 11 12 12 12 15 4 19 13 70 14 68 15 19 70 68 http://ecomputernotes.com
  • 12. Other Heap Operations decreaseKey(p, delta) : lowers the value of the key at position ‘p’ by the amount ‘delta’. Since this might violate the heap order, the heap must be reorganized with percolate up (in min heap) or down (in max heap). increaseKey(p, delta) : opposite of decreaseKey. remove(p) : removes the node at position p from the heap. This is done by first decreaseKey(p, ) and then performing deleteMin(). http://ecomputernotes.com
  • 13. Heap code in C++ template <class eType> class Heap { public: Heap( int capacity = 100 ); void insert( const eType & x ); void deleteMin( eType & minItem ); const eType & getMin( ); bool isEmpty( ); bool isFull( ); int Heap<eType>::getSize( ); http://ecomputernotes.com
  • 14. Heap code in C++ private: int currentSize; // Number of elements in heap eType* array; // The heap array int capacity; void percolateDown( int hole ); }; http://ecomputernotes.com
  • 15. Heap code in C++ #include &quot;Heap.h“ template <class eType> Heap<eType>::Heap( int capacity ) { array = new etype[capacity + 1]; currentSize=0; } http://ecomputernotes.com
  • 16. Heap code in C++ // Insert item x into the heap, maintaining heap // order. Duplicates are allowed. template <class eType> bool Heap<eType>::insert( const eType & x ) { if( isFull( ) ) { cout << &quot;insert - Heap is full.&quot; << endl; return 0; } // Percolate up int hole = ++currentSize; for(; hole > 1 && x < array[hole/2 ]; hole /= 2) array[ hole ] = array[ hole / 2 ]; array[hole] = x; } http://ecomputernotes.com
  • 17. Heap code in C++ template <class eType> void Heap<eType>::deleteMin( eType & minItem ) { if( isEmpty( ) ) { cout << &quot; heap is empty. “ << endl ; return; } minItem = array[ 1 ]; array[ 1 ] = array[ currentSize-- ]; percolateDown( 1 ); } http://ecomputernotes.com

Editor's Notes

  • #3: Star of lecture 31
  • #7: End of lecture 30
  • #18: End of lecture 31, start of 32